package com.sali.b_design_pattern.g_singleton_pattern.lesson_destroy_singleton.a_reflect.safe.change_non_param_construct;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @description: 反射能够破坏单例设计模式
 * @date: 2023/3/19 14:33.
 * @version: 1.0
 */
public class Test {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 拿到类对象
        Class<HungrySingleton3> hungrySingleton2Class = HungrySingleton3.class;
        // 通过返回获取构造函数
        Constructor<HungrySingleton3> declaredConstructor = hungrySingleton2Class.getDeclaredConstructor(null);
        // 设定允许通过反射访问的私有构造函数
        declaredConstructor.setAccessible(true);

        HungrySingleton3 hungrySingleton1 = declaredConstructor.newInstance();
        HungrySingleton3 hungrySingleton3 = declaredConstructor.newInstance();

        System.out.println(hungrySingleton1 == hungrySingleton3);
    }

}
